package com.itheima.controller;

import com.itheima.domain.Book;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Restful风格就是在url中仅指出操作对象，不指出操作行为，操作行为由请求方式指出
 *
 * 基于Restful风格的综合小案例
 */
@RestController     //封装@Controller和@ResponseBody
@RequestMapping("/books")
public class BookController {

    /**
     * 模拟将数据保存
     * @param book
     * @return
     */
    @PostMapping
    public String save(@RequestBody Book book) {
        System.out.println("book save ==> " + book);

        return "{'module':'book save success'}";
    }

    /**
     * 获取全部数据
     */
    @GetMapping
    public List<Book> getAll() {

        System.out.println("book getAll is Running...");

        List<Book> bookList = new ArrayList<Book>();

        Book book1 = new Book();
        book1.setType("计算机");
        book1.setName("SpringMvc入门教程");
        book1.setDescription("小试牛刀");
        bookList.add(book1);

        Book book2 = new Book();
        book2.setType("计算机");
        book2.setName("SpringMvc实战教程");
        book2.setDescription("一代宗师");
        bookList.add(book2);

        Book book3 = new Book();
        book3.setType("计算机系列丛书");
        book3.setName("SpringMvc实战进阶教程");
        book3.setDescription("一代宗师呕心之作");
        bookList.add(book3);


        return bookList;
    }
}
